package com.dgut.service.background.service;

import com.dgut.service.background.dao.UserDao;
import com.dgut.service.background.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserService {
    @Autowired
    private UserDao userDao;

    public Page<User> findAllUser(int page){
        Pageable pageable = PageRequest.of(page, 10);
        Page<User> users = userDao.findAll(pageable);
        return users;
    }

    public User findByUserId(int userId){
        return userDao.findByUserId(userId);
    }

    public void deleteOneUser(int userId){
        userDao.deleteById(userId);
    }

    public Page<User> findByUserName(String userName, int page){
        Pageable pageable = PageRequest.of(page, 10);
        return userDao.findByNameLike("%"+userName+"%",pageable);
    }

    public List<String> findOpenidByUserName(String userName){
        return userDao.findOpenidByName("%"+userName+"%");
    }

    public void saveOneUser(User user){
        userDao.save(user);
    }

    public User findByOpenId(String openId){
        User user = userDao.findUserByOpenid(openId);
        //判断用户账号是否已注销
        if (user == null){
            user = new User();
            user.setUserId(0);
            user.setOpenid("暂无数据");
            user.setIdNumber("暂无数据");
            user.setName("暂无数据");
            user.setPhone("暂无数据");
            user.setHealth("暂无数据");
            user.setRoomId("暂无数据");
            user.setPhotoPath("暂无数据");
            user.setSex("暂无数据");
            user.setYear("暂无数据");
        }
        return user;
    }
}
